import { DateTime } from 'luxon'
import type { HasMany } from '@adonisjs/lucid/types/relations'
import { BaseModel, column, hasMany } from '@adonisjs/lucid/orm'

import { isDev } from '#utils/env'
import User from '#models/user'
import Dict from '#models/dict'

export default class Shop extends BaseModel {
  @column({ isPrimary: true })
  declare id: number

  /** 父级门店ID */
  @column()
  declare parentId: number

  /** 门店Logo */
  @column({
    serialize(value) {
      const baseUrl = isDev ? 'http://localhost:3333' : 'https://api.mayday9.com'
      return baseUrl + value
    },
  })
  declare logo: string

  /** 门店名称 */
  @column()
  declare name: string

  /** 门店地址 */
  @column()
  declare address: string

  /** 门店手机号 */
  @column()
  declare mobilePhone: string

  /** 门店电话 */
  @column()
  declare phone: string

  /** 门店营业 */
  @column({
    serialize(value) {
      return value.slice(0, 5)
    },
  })
  declare openTime: string

  /** 闭店时间 */
  @column({
    serialize(value) {
      return value.slice(0, 5)
    },
  })
  declare closeTime: string

  /** 门店等级 */
  @column()
  declare level: number

  @column()
  declare region: object

  @column()
  declare location: object

  /** 小程序是否显示 */
  @column()
  declare minShow: boolean

  /** 门店状态 */
  @column()
  declare status: number

  /** 门店通知 */
  @column()
  declare notice: string

  /** 门店备注 */
  @column()
  declare memo: string

  @hasMany(() => User)
  declare users: HasMany<typeof User>

  @hasMany(() => Dict)
  declare dicts: HasMany<typeof Dict>

  @column.dateTime({
    autoCreate: true,
    serialize: (value) => value.toFormat('yyyy-MM-dd HH:mm:ss'),
  })
  declare createTime: DateTime

  @column()
  declare createUser: string

  @column.dateTime({
    autoCreate: true,
    autoUpdate: true,
    serialize: (value) => value.toFormat('yyyy-MM-dd HH:mm:ss'),
  })
  declare updateTime: DateTime

  @column()
  declare updateUser: string

  @column.dateTime({
    columnName: 'deleteTime',
    serialize: (value) => value?.toFormat('yyyy-MM-dd HH:mm:ss'),
  })
  declare deleteTime: DateTime
}
